package com.smarth.zhizulian.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.app.BaseActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.BaseEntity;
import com.smarth.zhizulian.bean.article.ArticleShare;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.ShareUtils;
import com.smarth.zhizulian.utils.UserUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * Created by 18865 on 2017/12/30.
 */

public class WebViewActivity extends BaseActivity {
    private WebView webView;
    public static final String INTENT_URL = "INTENT_URL";
    public static final String INTENT_TITLE = "INTENT_TITLE";
    public static final String INTENT_BUNDLE = "INTENT_BUNDLE";

    private String mUrl;
    private String mTitle;
    private String mBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mUrl = getIntent().getStringExtra(INTENT_URL);
        mTitle = getIntent().getStringExtra(INTENT_TITLE);
        mBundle = getIntent().getStringExtra(INTENT_BUNDLE);
        setTitlebar(mTitle, true);
        initViews();
        initWebView();
    }

    private void initViews() {
        webView = (WebView) findViewById(R.id.wv_main);
    }

    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        webView.requestFocus();

//        webView.setOnKeyListener(new View.OnKeyListener() {        // webview can go back
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
//                    webView.goBack();
//                    return true;
//                }
//                return false;
//            }
//        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                Uri uri = Uri.parse(url);
//                if (uri.getScheme().equals("tel")) {
//                    CommonUtils.callPhone(uri.getHost(), GongQiuActivity.this);
//                } else {
//                    view.loadUrl(url);   //在当前的webview中跳转到新的url
//                }
                view.loadUrl(url);   //在当前的webview中跳转到新的url
                return true;
            }
        });

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDefaultTextEncodingName("utf-8");
        webView.addJavascriptInterface(new JSArticle(), "article");

        webView.loadUrl(mUrl);
    }

//    public static void open(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        context.startActivity(i);
//    }
//
//    public static void openForStandard(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        i.putExtra(INTENT_URL, "http://www.mainsm.com/h5/rule-detail.html");
//        i.putExtra(INTENT_TITLE, "新手必读");
//        context.startActivity(i);
//    }
//
//    public static void openForQuestion(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        i.putExtra(INTENT_URL, "http://www.mainsm.com/h5/rule-detail-2.html");
//        i.putExtra(INTENT_TITLE, "常见问题");
//        context.startActivity(i);
//    }
//
//    public static void openForMoneyCheats(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        i.putExtra(INTENT_URL, "http://www.mainsm.com/h5/rule-detail-4.html");
//        i.putExtra(INTENT_TITLE, "赚钱秘籍");
//        context.startActivity(i);
//    }
//
    public static void openForArticle(Context context, String title, String relationId) {
        Intent i = new Intent(context, WebViewActivity.class);
        i.putExtra(INTENT_URL, BaseConfig.NetWork.URL_ARTICLE + relationId);
//        i.putExtra(INTENT_URL, "http://192.168.1.8:8080/news-detail.html?id=" + relationId);
        i.putExtra(INTENT_TITLE, title);
        i.putExtra(INTENT_BUNDLE, relationId);
        context.startActivity(i);
    }
//
//    public static void openForDraw(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        i.putExtra(INTENT_URL, "http://www.mainsm.com/h5/user/choujiang.html?token=" + UserUtils.getToken());
//        i.putExtra(INTENT_TITLE, "幸运抽奖");
//        context.startActivity(i);
//    }
//
//    public static void openForAgreement(Context context) {
//        Intent i = new Intent(context, WebViewActivity.class);
//        i.putExtra(INTENT_URL, "http://www.mainsm.com/h5/user/agreement.html");
//        i.putExtra(INTENT_TITLE, "登录协议");
//        context.startActivity(i);
//    }

    public class JSArticle {
        @JavascriptInterface
        public void shareTimeLine(long relationId) {
            KumaLog.kuma("relationId:" + relationId);
            //朋友圈
            if (UserUtils.isLogin()) {
                KumaHttp.getService().articleShare(mBundle)
                        .compose(RxCompos.<ArticleShare>getBELoginCompos())
                        .subscribe(new ProgressObserver<ArticleShare>(mContext) {
                            @Override
                            public void onNext(ArticleShare articleShare) {
                                ShareUtils.shareToMomentWebPage(articleShare.getTitle(), articleShare.getDesc(), articleShare.getImage(), articleShare.getUrl(), new PlatformActionListener() {
                                    @Override
                                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                        KumaToast.show(mContext, "分享成功");
                                        KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHAREARTICLECIRCLE)
                                                .compose(RxCompos.<Long>getBEAllLoginCompos())
                                                .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext, false) {
                                                    @Override
                                                    public void onNext(BaseEntity<Long> o) {

                                                    }
                                                });
                                    }

                                    @Override
                                    public void onError(Platform platform, int i, Throwable throwable) {
                                        KumaToast.show(mContext, "分享失败");
                                    }

                                    @Override
                                    public void onCancel(Platform platform, int i) {
                                        KumaToast.show(mContext, "取消分享");
                                    }
                                });
                            }
                        });
            } else {
                LoginActivity.open(mContext);
            }
        }

        @JavascriptInterface
        public void shareFriend(long relationId) {
            KumaLog.kuma("relationId:" + relationId);
            //微信
            if (UserUtils.isLogin()) {
                KumaHttp.getService().articleShare(mBundle)
                        .compose(RxCompos.<ArticleShare>getBELoginCompos())
                        .subscribe(new ProgressObserver<ArticleShare>(mContext) {
                            @Override
                            public void onNext(ArticleShare articleShare) {
                                ShareUtils.shareToWechatWebPage(articleShare.getTitle(), articleShare.getDesc(), articleShare.getImage(), articleShare.getUrl(), new PlatformActionListener() {
                                    @Override
                                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                        KumaToast.show(mContext, "分享成功");
                                        KumaHttp.getService().taskFinishTask(BaseConfig.Constant.TASK_SHAREARTICLE)
                                                .compose(RxCompos.<Long>getBEAllLoginCompos())
                                                .subscribe(new ProgressObserver<BaseEntity<Long>>(mContext, false) {
                                                    @Override
                                                    public void onNext(BaseEntity<Long> o) {

                                                    }
                                                });
                                    }

                                    @Override
                                    public void onError(Platform platform, int i, Throwable throwable) {
                                        KumaToast.show(mContext, "分享失败");
                                    }

                                    @Override
                                    public void onCancel(Platform platform, int i) {
                                        KumaToast.show(mContext, "取消分享");
                                    }
                                });
                            }
                        });
            } else {
                LoginActivity.open(mContext);
            }
        }
    }
}
